package fr.cpe.j2ee.projet.war;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import fr.cpe.j2ee.projet.ejb.ConversationSession;
import fr.cpe.j2ee.projet.ejb.EmployeeSession;
import fr.cpe.j2ee.projet.ejb.ProjectSession;
import fr.cpe.j2ee.projet.model.Employee;
import fr.cpe.j2ee.projet.model.Project;

public class ConversationRequestHandler {
	private Map<Integer, ConversationRequest> currentConversationRequests; // La clé c'est l'ID de la personne qui reçoit l'invitation
	private Map<Integer, String> currentMessage; // La clé c'est l'ID de la personne qui reçoit l'invitation
	
	@EJB
	private EmployeeSession employeeSession;

	@EJB
	private ConversationSession conversationSession;
	
	@EJB
	private ProjectSession projectSession;
	
	public ConversationRequestHandler() {
	}
	
	@PostConstruct
	public void postContruct() {
		currentConversationRequests = new HashMap<Integer, ConversationRequest>();
		currentMessage = new HashMap<Integer, String>();
	}
	
	public void addConversationRequest(Integer projectId, Integer employeeId) {		
		Project project = projectSession.getById(projectId.intValue());
		
		ConversationRequest conversationRequest = new ConversationRequest();
		conversationRequest.setEmployeeSender(((LoginBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("login")).getEmployee());
		conversationRequest.setProject(project);
		conversationRequest.setSentDate(new Date());
		
		currentConversationRequests.put(employeeId.intValue(), conversationRequest);
		FacesContext.getCurrentInstance().addMessage("growl", new FacesMessage("Demande de conversation envoyée"));	
	}
	
	public void checkConversationRequest(int empId) {
		// Cette méthode permet également de faire appel à la méthode getConversationRequest() car cette dernière
		// se trouve dans le même formulaire que la requête ajax poll de checkConversationRequest()
		String message = currentMessage.get(empId);
		if(message != null) {
			currentMessage.remove(empId);
			FacesContext.getCurrentInstance().addMessage("growl", new FacesMessage(message));
		}
	}
	
	public ConversationRequest getConversationRequest(int empId) {
		return currentConversationRequests.get(empId);
	}
	
	public void acceptConversationRequest() {
		Employee currentEmployee = ((LoginBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("login")).getEmployee();
		int empId = currentEmployee.getEmpId();
		System.out.println("acceptConversationRequest("+empId+")");
		ConversationRequest conversationRequest = currentConversationRequests.get(empId);
		
		HashMap<Employee,Boolean> participants = new HashMap<Employee, Boolean>();
		Employee empSender = conversationRequest.getEmployeeSender();
		participants.put(employeeSession.getById(empId), false);
		participants.put(empSender,true);
		
		conversationSession.create(
			new Date(), 
			null, 
			conversationRequest.getProject(), 
			participants
		);
		
		currentConversationRequests.remove(empId);
		
		currentMessage.put(empSender.getEmpId(), 
			"La conversation avec " + currentEmployee.getEmpFirstname() + " " 
			+ currentEmployee.getEmpLastname() 
			+ " dans le cadre du projet " 
			+ conversationRequest.getProject().getProName() + " a été acceptée"
		);
		
		try {
			ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    		context.redirect(context.getRequestContextPath() + "/xhtml/user/chatRoom.xhtml");
		} catch (IOException e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("La redirection a échoué !"));	
			e.printStackTrace();
		}
	}
	
	public void declineConversationRequest() {
		Employee currentEmployee = ((LoginBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("login")).getEmployee();
		int empId = currentEmployee.getEmpId();
		System.out.println("declineConversationRequest("+empId+")");
		ConversationRequest conversationRequest = currentConversationRequests.get(empId);
		
		currentConversationRequests.remove(empId);
		
		currentMessage.put(conversationRequest.getEmployeeSender().getEmpId(), 
			"La conversation avec " + currentEmployee.getEmpFirstname() + " " 
			+ currentEmployee.getEmpLastname() 
			+ " dans le cadre du projet " 
			+ conversationRequest.getProject().getProName() + " a été refusée"
		);
	}
}
